package com.fla.shapeevader.model.support;

import android.graphics.RectF;

import com.fla.shapeevader.model.Trajectory;

public class SinTrajectory extends Trajectory {
    private float temp = 0;
    private boolean inc = true;

    public SinTrajectory(float direction, float speed) {
        // direction ignored in this trajectory
        super(0, speed);
    }

    @Override
    public float getDx() {
        float dx = inc ? getSpeed() / 4 : -getSpeed() / 4;
        temp = inc ? temp + getSpeed() / 4 : temp - getSpeed() / 4;
        if (temp < -50) {
            inc = true;
        }
        if (temp > 50) {
            inc = false;
        }
        return dx;
    }

    @Override
    public float getDy() {
        return getSpeed() * (float) Math.random();
    }

    @Override
    public void recalculateParams(RectF bounds, float fullWidth, float fullHeight) {
        if(bounds.left < 0) {
            bounds.offset(-bounds.left, 0);
            inc = true;
            temp = 0;
        }
        
        if(bounds.right > fullWidth) {
            bounds.offset(fullWidth - bounds.right, 0);
            inc = false;
            temp = 0;
        }
        
        if (bounds.top > fullHeight) {
            // restart this shape
            float offsetX = (float) Math.random() * (fullWidth - bounds.right + bounds.left) - bounds.left;
            float offsetY = -fullHeight - (float) Math.random() * bounds.top;
            bounds.offset(offsetX, offsetY);
        }
    }
}
